public class h_8_ZeroEvenOdd {
    private int n;
    private int i=0;
    private int flag = 0;
    private Object lock = new Object();
    public h_8_ZeroEvenOdd(int n) {
        this.n = n;
    }

    public void zero() throws InterruptedException {
        for(int j=1;j<=n;j++){
            synchronized(lock){
                while(flag!=0){
                    lock.wait();
                }
                System.out.print(0);
                flag=1;
                i=j;
                lock.notifyAll();
            }
        }

    }

    public void even() throws InterruptedException {
        for(int j=2;j<=n;j=j+2){
            synchronized(lock){
                while(flag!=1||i!=j){
                    lock.wait();
                }
                System.out.print(j);
                flag=0;
                lock.notifyAll();
            }
        }
    }

    public void odd() throws InterruptedException {
        for(int j=1;j<=n;j=j+2){
            synchronized(lock){
                while(flag!=1||i!=j){
                    lock.wait();
                }
                System.out.print(j);
                flag=0;
                lock.notifyAll();
            }
        }
    }
}
